import { PrismaService } from '@app/prisma';
import { ResultEntity } from '@app/prisma/entitys/result.entity';
import { UserEntity } from '@app/prisma/entitys/user.entity';
import { ForbiddenError } from '@nestjs/apollo';
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver } from '@nestjs/graphql';
import { GqlAuthGuard, GqlCurrentUser } from 'src/auth/auth.guard';

@Resolver()
export class UserResolver {
    constructor(
        private readonly prismaService: PrismaService
    ) { }

    @Mutation(() => ResultEntity)
    @UseGuards(GqlAuthGuard)
    async updInformation(
        @Args("address") address: string,
        @Args("account") account: string,
        @Args("name") name: string,
        @GqlCurrentUser() user: UserEntity
    ) {
        const find = await this.prismaService.user.findUnique({
            where: { account }
        })
        if (!!find) {
            if (find.account !== user.account) {
                throw new ForbiddenError("手机号已被占用")
            }
        }
        await this.prismaService.user.update({
            where: {
                id: user.id
            },
            data: {
                account,
                address,
                name
            }
        })
        return new ResultEntity("success")
    }
}
